package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.widget.Toast;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.widget.itemView.CitySearchView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.InsureModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SourcePathInfo;
import com.tianhui.consignortwofei.mvp.model.enty.insure.InsureQueryInfo;

import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/9/24
 * @description
 **/
public class ChangeDressActivity extends BaseActivity {
//    @BindView(R.id.activity_insure_detail_insureAmountTextView)
//    TextView mInsureAmountTextView;
//    @BindView(R.id.activity_insure_detail_insurePriceTextView)
//    TextView mInsurePriceTextView;
//    @BindView(R.id.activity_insure_detail_insureNameTextView)
//    TextView mInsureNameTextView;
//    @BindView(R.id.activity_insure_detail_startDateTextView)
//    TextView mStartDateTextView;
//    @BindView(R.id.activity_insure_detail_endDateTextView)
//    TextView mEndDateTextView;

    protected CitySearchView mDeliverCitySearchView;
    protected CitySearchView mDestinationCitySearchView;

    private InsureModel mInsureModel;
    private InputItemView inpiv_zhuanghuo;
    private InputItemView inpiv_shouhuo;
    private OrderInfo orderInfo;
    //    private Button but_up_info;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_dress;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "修改地址";
    }

    @Override
    public void init() {

        initView();

        mInsureModel = new InsureModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            orderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (orderInfo != null) {
//                String dispatchno = orderInfo.dispatchno;
//                requestInsureQuery(orderInfo);


                initZhuangHuo(orderInfo);
                initXieHuo(orderInfo);



            }
        }

    }



    private void initZhuangHuo(OrderInfo orderInfo) {
        try {
            SourcePathInfo sourcePathInfo = new SourcePathInfo();
            sourcePathInfo.placeprovince = orderInfo.deliverprovince;
            sourcePathInfo.placecity = orderInfo.delivercity;
            sourcePathInfo.placecounty = orderInfo.delivercounty;
            sourcePathInfo.placecitycode = orderInfo.delivercitycode;
            sourcePathInfo.placeparentcode = orderInfo.delivercitycode;
            sourcePathInfo.address=orderInfo.deliverprovince+orderInfo.delivercity+orderInfo.delivercounty;
            setSelectPathInfo(KeyValue.RESULT_DELIVER, sourcePathInfo, false);
            inpiv_zhuanghuo.setContent(orderInfo.deliverdetails);
        }catch (Exception e){
            Toast.makeText(ChangeDressActivity.this, "获取原装货地址失败", Toast.LENGTH_SHORT).show();
        }


    }


    private void initXieHuo(OrderInfo orderInfo) {
        try {
            SourcePathInfo sourcePathInfo = new SourcePathInfo();
            sourcePathInfo.placeprovince = orderInfo.destinationprovince;
            sourcePathInfo.placecity = orderInfo.destinationcity;
            sourcePathInfo.placecounty = orderInfo.destinationcounty;
            sourcePathInfo.placecitycode = orderInfo.destinationcitycode;
            sourcePathInfo.placeparentcode = orderInfo.destinationcitycode;
            sourcePathInfo.address=orderInfo.destinationprovince+orderInfo.destinationcity+orderInfo.destinationcounty;
            setSelectPathInfo(KeyValue.RESULT_DESTINATION, sourcePathInfo, false);
            inpiv_shouhuo.setContent(orderInfo.destinationdetails);
        }catch (Exception e){
            Toast.makeText(ChangeDressActivity.this, "获取目的地址失败", Toast.LENGTH_SHORT).show();
        }
    }


//    private void requestInsureQuery(OrderInfo orderInfo) {
//        Map<String, String> map = new HashMap<>();
//        map.put("dispatchno", orderInfo.dispatchno);
//        mInsureModel.insureQueryModel(this, true, map, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<InsureQueryInfo>>() {
//            @Override
//            public void onNext(CommonResponse<InsureQueryInfo> commonResponse) {
//                if (commonResponse.getCode() == 20000) {
//                    InsureQueryInfo insureQueryInfo = commonResponse.getData();
//                    if (insureQueryInfo != null) {
//                        requestInsureInfo(insureQueryInfo);
//                    }
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//
//            }
//        });
//    }
//
//    private void requestInsureInfo(InsureQueryInfo insureQueryInfo) {
//        mInsureModel.insureInfoModel(this, true, insureQueryInfo, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<InsureData>>() {
//            @Override
//            public void onNext(CommonResponse<InsureData> commonResponse) {
//                if (commonResponse.getCode() == 20000) {
//                    InsureData insureData = commonResponse.getData();
//                    if (insureData != null && !TextUtils.isEmpty(insureData.status) && insureData.status.equals("1001")) {
//                        InsureResultInfo insureResultInfo = insureData.result;
//                        if (insureResultInfo != null) {
//                            InsureResultDataInfo resultDataInfo = insureResultInfo.data;
//                            if (resultDataInfo != null) {
//                                mInsureAmountTextView.setText(resultDataInfo.amount);
//                                mInsurePriceTextView.setText("保费：" + resultDataInfo.payPrem);
//                                mStartDateTextView.setText("起始日期：" + resultDataInfo.startDate);
//                                mEndDateTextView.setText("截止日期：" + resultDataInfo.endDate);
//                                InsureInfo insureInfo = resultDataInfo.applicantInfo;
//                                if (insureInfo != null) {
//                                    mInsureNameTextView.setText("投保人：" + insureInfo.name);
//                                }
//                            }
//                        }
//                    }
//
//
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//
//            }
//        });
//    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }



    private void initView() {
        mDeliverCitySearchView = findViewById(R.id.csv_zhuanghuo);
        mDestinationCitySearchView = findViewById(R.id.csv_shouhuo);

        inpiv_zhuanghuo = findViewById(R.id.inpiv_zhuanghuo);
        inpiv_shouhuo = findViewById(R.id.inpiv_shouhuo);
//        but_up_info = findViewById(R.id.but_up_info);

        mDeliverCitySearchView.setTitle("装货地");
        mDeliverCitySearchView.setContentHint("请输入或选择装货地");
        mDestinationCitySearchView.setTitle("目的地");
        mDestinationCitySearchView.setContentHint("请输入或选择目的地");

        mDeliverCitySearchView.setOnCitySearchListener(new CitySearchView.OnCitySearchListener() {
            @Override
            public void citySearchCallback(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                sourcePathInfo.placeparentcode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DELIVER, sourcePathInfo, true);

            }
        });

        mDeliverCitySearchView.setOnCitySearchSelectListener(new CitySearchView.OnCitySearchSelectListener() {
            @Override
            public void onCitySelect(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                sourcePathInfo.placeparentcode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DELIVER, sourcePathInfo, false);
            }
        });


        mDestinationCitySearchView.setOnCitySearchListener(new CitySearchView.OnCitySearchListener() {
            @Override
            public void citySearchCallback(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                sourcePathInfo.placeparentcode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DESTINATION, sourcePathInfo, true);
            }
        });

        mDestinationCitySearchView.setOnCitySearchSelectListener(new CitySearchView.OnCitySearchSelectListener() {
            @Override
            public void onCitySelect(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                sourcePathInfo.placeparentcode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DESTINATION, sourcePathInfo, false);
            }
        });




    }


    //装货
    SourcePathInfo zhuangHuoDress;
    SourcePathInfo xieHuoDress;
    private void setSelectPathInfo(int resultCode, SourcePathInfo pathInfo, boolean isNeedReprocessingPathInfo) {
        if (pathInfo == null) return;
        if (resultCode == KeyValue.RESULT_DELIVER) {
//            mDataProcess.processPlaceSelectPathInfo(mGoodsInfo, pathInfo);
//            mDataProcess.processDeliverySelectPathInfo(mGoodsInfo, pathInfo);
            mDeliverCitySearchView.setIsNeedFilterCity(isNeedReprocessingPathInfo);
            mDeliverCitySearchView.setContent(pathInfo.address);
            zhuangHuoDress=pathInfo;
        } else if (resultCode == KeyValue.RESULT_DESTINATION) {
//            mDataProcess.processDestinationSelectPathInfo(mGoodsInfo, pathInfo);
            mDestinationCitySearchView.setIsNeedFilterCity(isNeedReprocessingPathInfo);
            mDestinationCitySearchView.setContent(pathInfo.address);
            xieHuoDress=pathInfo;
        }

    }

    @OnClick(R.id.but_up_info)
    public void commit() {
        String zhuangText = inpiv_zhuanghuo.getContent();
        String shouText = inpiv_shouhuo.getContent();

        if (zhuangHuoDress == null) {
            Toast.makeText(ChangeDressActivity.this, "请先选择发货地", Toast.LENGTH_SHORT).show();
            return;
        }
        if (xieHuoDress  == null) {
            Toast.makeText(ChangeDressActivity.this, "请先选择目的地", Toast.LENGTH_SHORT).show();
            return;
        }


        Map<String, String> map = new HashMap<>();
        map.put("code1", zhuangHuoDress.placecitycode);
        map.put("code2", zhuangHuoDress.placecitycode);
        map.put("code3", xieHuoDress.placecitycode);

        map.put("codename1", zhuangHuoDress.placecounty);
        map.put("codename2", zhuangHuoDress.placecounty);
        map.put("codename3", xieHuoDress.placecounty);

        map.put("codename1a", zhuangHuoDress.placeprovince);
        map.put("codename2a", zhuangHuoDress.placeprovince);
        map.put("codename3a", xieHuoDress.placeprovince);

        map.put("codename1b", zhuangHuoDress.placecity);
        map.put("codename2b", zhuangHuoDress.placecity);
        map.put("codename3b", xieHuoDress.placecity);


        map.put("codename1c", zhuangText);
        map.put("codename2c", zhuangText);
        map.put("codename3c", shouText);

        map.put("id", orderInfo.orderid);


        if (OrderType.TYPE_DISPATCH_DINGJIN==orderInfo.dingJinType){//有定金
            mInsureModel.changeDressAllDingjin(this, true, map, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<InsureQueryInfo>>() {
                @Override
                public void onNext(CommonResponse commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        EventBusManage.refreshOrderList();
                        finish();
                        Toast.makeText(ChangeDressActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
//                    InsureQueryInfo insureQueryInfo = commonResponse.getData();
//                    if (insureQueryInfo != null) {
//                        requestInsureInfo(insureQueryInfo);
//                    }
                    }
                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    Toast.makeText(ChangeDressActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
            });
        }else {//无订金
            mInsureModel.changeDressAll(this, true, map, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<InsureQueryInfo>>() {
                @Override
                public void onNext(CommonResponse commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        EventBusManage.refreshOrderList();
                        finish();
                        Toast.makeText(ChangeDressActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
//                    InsureQueryInfo insureQueryInfo = commonResponse.getData();
//                    if (insureQueryInfo != null) {
//                        requestInsureInfo(insureQueryInfo);
//                    }
                    }
                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    Toast.makeText(ChangeDressActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
            });
        }



//        Toast.makeText(ChangeDressActivity.this, "点击提交", Toast.LENGTH_SHORT).show();
    }





}
